package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.CustomerProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CategoryMapper;
import com.ruoyi.system.domain.Category;
import com.ruoyi.system.service.ICategoryService;

/**
 * 商品分类Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-08
 */
@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询商品分类
     *
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public Category selectCategoryById(Long id) {
        return categoryMapper.selectCategoryById(id);
    }

    /**
     * 查询商品分类列表
     *
     * @param category 商品分类
     * @return 商品分类
     */
    @Override
    public List<Category> selectCategoryList(Category category) {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增商品分类
     *
     * @param category 商品分类
     * @return 结果
     */
    @Override
    public int insertCategory(Category category) {
        category.setCreateTime(DateUtils.getNowDate());
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改商品分类
     *
     * @param category 商品分类
     * @return 结果
     */
    @Override
    @CacheEvict(value = "categoryCache", key = "#category.customerName")  // 清除对应 customerName 的缓存
    public int updateCategory(Category category) {
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除商品分类
     *
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    @CacheEvict(value = "categoryCache", allEntries = true)  // 强制清空整个 categoryCache 缓存
    public int deleteCategoryByIds(Long[] ids) {
        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除商品分类信息
     *
     * @param id 商品分类主键
     * @return 结果
     */
    @Override
    @CacheEvict(value = "categoryCache", allEntries = true)  // 强制清空整个 categoryCache 缓存
    public int deleteCategoryById(Long id) {
        return categoryMapper.deleteCategoryById(id);
    }


    @Override
    @Cacheable(value = "categoryCache", key = "#customerName")  // 缓存 Key = 客户名
    public List<Category> selectByCustomerName(String customerName) {
        // 处理null或"null"的两种情况
        if (StringUtils.isEmpty(customerName) || "null".equalsIgnoreCase(customerName)) {
            return getDefaultCustomerProfile();
        }

        // 尝试查找指定客户名的记录
        List<Category> customerProfile = categoryMapper.selectByCustomerName(customerName);

        // 如果未找到，返回默认记录
        return customerProfile != null ? customerProfile : getDefaultCustomerProfile();
    }

    @Override
    public List<Category> selectByCustomerNameISNull() {
        return categoryMapper.selectByCustomerNameISNull();
    }

    // 提取重复操作为独立方法
    private List<Category> getDefaultCustomerProfile() {
        return categoryMapper.selectByCustomerNameISNull();
    }
}
